package com.pvdnc.tapkit.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.util.concurrent.atomic.AtomicReference;

public class LocalBroadcast {
    private static final AtomicReference<LocalBroadcast> sInstance = new AtomicReference<>();

    public static void systemReady(Context context) {
        if(sInstance.get()!=null){
            return;
        }
        sInstance.set(new LocalBroadcast(context));
    }

    public static LocalBroadcast getInstance() {
        return sInstance.get();
    }

    private LocalBroadcastManager mManager;

    private LocalBroadcast(Context context) {
        mManager= LocalBroadcastManager.getInstance(context);
    }

    public void send(Intent intent){
        mManager.sendBroadcast(intent);
    }

    public void register(BroadcastReceiver receiver, IntentFilter filter){
        mManager.registerReceiver(receiver,filter);
    }

    public void unregister(BroadcastReceiver receiver){
        mManager.unregisterReceiver(receiver);
    }
}
